//路由鉴权:就是路由能不能被访问到权限的设置,用vue的全局路由守卫来实现

//引入路由器
import router from "@/router/index.ts";
//引入仓库
import Pinia from "@/store/index.ts"
import useUserStore from "@/store/modules/user.ts";

//引入进度条
import progress from "nprogress";
//引入进度条样式
import "nprogress/nprogress.css";
//去掉进度条的右下角显示的旋转小圆球

progress.configure({showSpinner: false});

let userStore = useUserStore(Pinia);

//未登录可访问的名单
let whiteList = ["/","/home","/hospital/register","/hospital/detail","/hospital/notice","/hospital/close","/hospital/search"];

//前置守卫
router.beforeEach((to, from, next) => {
    progress.start();
    //动态设置浏览器网页标签标题
    document.title = `贵医通-${to.meta.title}`;
    //根据token判断用户是否登录
    let token = userStore.userInfo.token;
    if (token) {
        next();
    } else {
        //未登录只能访问部分页面
        if (whiteList.includes(to.path)) {
            next();
        } else {
            //提示登录
            userStore.visible = true;
            //转跳首页登录,并把原本要去的组件的位置的参数给到home登录页,方便登录成功再去该参数用于回转到原本想去的地方
            next({path: "/home",query:{redirect: to.fullPath}});
        }
    }
});

//后置守卫
router.afterEach((to, from) => {
    progress.done();
});